/**
 * Copyright (c) 2009, ERICSSON AB
 *  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or 
 * without modification, are permitted provided that the following 
 * conditions are met:
 * - Redistributions of source code must retain the above copyright 
 *   notice, this list of conditions and the following disclaimer. 
 * - Redistributions in binary form must reproduce the above copyright 
 *   notice, this list of conditions and the following disclaimer in 
 *   the documentation and/or other materials provided with the 
 *   distribution.
 * - Neither the name ERICSSON AB nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 */
package com.ericsson.labs.wave.messy.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.logging.Logger;

public class HttpHelperUtil {

   private static final Logger log = Logger.getLogger(HttpHelperUtil.class
         .getName());

   /**
    * Sends a synchronous HTTP GET request and returns the associated HTTP
    * response content
    * 
    * @param url
    *           the destination of the request
    * @return the content of the response
    * @throws IOException
    * @throws MalformedURLException
    */
   public static String sendSynchronousHttpGetRequest(String url)
         throws MalformedURLException, IOException {
      // Opens a connection on the URL
      HttpURLConnection connection = (HttpURLConnection) new URL(url)
            .openConnection();

      // Creates a reader on the input stream of this connection
      BufferedReader reader = new BufferedReader(new InputStreamReader(
            connection.getInputStream()));

      // Retrieves HTTP response
      StringBuilder result = new StringBuilder();
      try {
         String inputLine = null;
         while ((inputLine = reader.readLine()) != null) {
            result.append(inputLine);
         }
      } catch (Exception e) {
         log.warning(e.getMessage());
      } finally {
         try {
            reader.close();
         } catch (Exception e) {
            log.warning(e.getMessage());
         }
      }
      return result.toString();
   }

   /**
    * URL-encodes a string using UTF-8 encoding.
    * 
    * @param string
    *           the string to URL-encode.
    * @return the URL-encoded string
    * @throws UnsupportedEncodingException
    */
   public static String urlEncode(String string)
         throws UnsupportedEncodingException {
      return URLEncoder.encode(string, "UTF-8");
   }

}